package course.dao.impl;

import course.dao.BaseDao;
import course.dao.ZhuanyeDao;
import course.pojo.entity.Zhuanye;
import course.pojo.vo.Page;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author HLF
 * @date 2022/6/30 21:19
 */
public class ZhuanyeDaoImpl extends BaseDao<Zhuanye> implements ZhuanyeDao {
    @Override
    public Zhuanye getById(Integer zid, Connection connection) {
        String sql = "select * from t_zhuanye where zid = ?;";
        return getOneBean(connection, sql, zid);
    }

    @Override
    public int addZhuanye(Zhuanye zhuanye, Connection connection) {
        String sql = "insert into t_zhuanye values(?,?,?,?);";
        return insert(connection, sql, zhuanye.getZid(), zhuanye.getZname(), zhuanye.getZdesc(), zhuanye.getCid());
    }

    @Override
    public int updateById(Zhuanye zhuanye, Connection connection) {
        String sql = "update t_zhuanye set zname = ?,zdesc = ?,cid = ? where zid = ?";
        return update(connection, sql, zhuanye.getZname(), zhuanye.getZdesc(), zhuanye.getCid(), zhuanye.getZid());
    }

    @Override
    public int deleteById(Integer zid, Connection connection) {
        String sql = "delete from t_zhuanye where zid = ?;";
        return update(connection, sql, zid);
    }

    @Override
    public List<Zhuanye> getList(Connection connection) {
        String sql = "select * from t_zhuanye;";
        return getBeanList(connection, sql);
    }

    /**/

    @Override
    public int deleteSelect(List<Integer> ids, Connection connection) {
        String sql = "delete from t_zhuanye where zid in(?)";

        String sqlIN = ids.stream()
                .map(x -> String.valueOf(x))
                .collect(Collectors.joining(",", "(", ")"));

        sql = sql.replace("(?)", sqlIN);

        try {
            PreparedStatement preparedStatement = connection.prepareStatement(sql);

            return preparedStatement.executeUpdate();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public long count(Connection connection) {
        String sql = "select count(*) from t_zhuanye;";
        return (long) getCount(connection, sql);
    }

    /**
     * 分页
     *
     * @param pageNo 当前页码数
     * @return 返回通用的分页对象
     */
    @Override
    public Page<Zhuanye> page(Integer pageNo, Connection connection) {
        Page<Zhuanye> page = new Page<>();
        Integer pageSize = page.getPageSize();

        String sql = "select * from t_zhuanye limit ?,?;";
        List<Zhuanye> list = getBeanList(connection, sql, (pageNo - 1) * pageSize, pageSize);

        page.setPageNo(pageNo);
        page.setPageSize(pageSize);
        page.setList(list);
        long total = this.count(connection);
        page.setTotal(total);

        int pages = (int) Math.ceil((double) total / pageSize);
        page.setPages(pages);

        return page;
    }

    /**
     * 按照学院关键字查找，并返回分页对象
     */
    @Override
    public Page<Zhuanye> query(String zname, Integer pageNo, Connection connection) {
        Page<Zhuanye> page = new Page<>();
        Integer pageSize = page.getPageSize();

        String sql = "select * from t_zhuanye where zname like ? limit ?,?";
        String key = "%" + zname + "%";
        List<Zhuanye> list = getBeanList(connection, sql, key, (pageNo - 1) * pageSize, pageSize);

        String sql2 = "select count(*) from t_zhuanye where zname like ?;";
        long count = (long) getCount(connection, sql2, key);

        page.setPageNo(pageNo);
        page.setPageSize(pageSize);
        page.setList(list);
        page.setTotal(count);

        int pages = (int) Math.ceil((double) count / pageSize);
        page.setPages(pages);

        return page;
    }

    @Override
    public List<Zhuanye> getListByCid(Integer cid, Connection connection) {
        String sql = "select * from t_zhuanye where cid = ?;";
        return getBeanList(connection, sql, cid);
    }

    @Override
    public Zhuanye getByZname(String zname, Connection connection) {
        String sql = "select * from t_zhuanye where zname = ?;";
        return getOneBean(connection,sql,zname);
    }

    @Override
    public List<Zhuanye> selectBathByIds(List<Integer> ids, Connection connection) {
        String sql = "select * from t_zhuanye where zid in(?)";

        String sqlIN = ids.stream()
                .map(x -> String.valueOf(x))
                .collect(Collectors.joining(",", "(", ")"));

        sql = sql.replace("(?)", sqlIN);

        try {
            PreparedStatement preparedStatement = connection.prepareStatement(sql);
            ResultSet rs = preparedStatement.executeQuery();
            List<Zhuanye> zhuanyes = new ArrayList<>();
            while (rs.next()) {
                Zhuanye zhuanye = new Zhuanye();
                zhuanye.setZid(rs.getInt(1));
                zhuanye.setZname(rs.getString(2));
                zhuanye.setZdesc(rs.getString(3));
                zhuanye.setCid(rs.getInt(4));

                zhuanyes.add(zhuanye);
            }
            return zhuanyes;

        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
}
